package com.drondea.testclient.service;

import com.drondea.sms.channel.ChannelSession;
import com.drondea.sms.message.IMessage;
import com.drondea.sms.message.smgp30.msg.SmgpSubmitRequestMessage;
import com.drondea.sms.thirdparty.SmsAlphabet;
import com.drondea.sms.thirdparty.SmsDcs;
import com.drondea.sms.type.IMessageResponseHandler;
import com.drondea.testclient.panel.SmgpPanel;
import com.drondea.testclient.service.provider.SmgpClientMessageProvider;
import com.drondea.testclient.util.CommonThreadPoolFactory;
import com.drondea.testclient.util.counter.SmgpAtomicUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 发送服务类 2
 *
 * @author sun
 * 2019年4月23日 下午2:25:34
 */

@Slf4j
public class SmgpService extends AbstractTcpService {

    private static final SmgpPanel PANEL = SmgpPanel.getInstance();
    private static final SmgpService INSTANCE = new SmgpService();

    private SmgpService() {}
    public static SmgpService getInstance() {
        return INSTANCE;
    }
    
    @Override
    public void sendSms(String mobile, String content, String subCode) {
        try {
            SmgpSubmitRequestMessage requestMessage = new SmgpSubmitRequestMessage();
            requestMessage.setMsgContent(content);
            requestMessage.setServiceId("1");
            requestMessage.setMsgSrc("AAAA");
            requestMessage.setSrcTermId(subCode);
            requestMessage.setNeedReport(true);
            requestMessage.setDestTermIdArray(new String[]{mobile});
            requestMessage.setReserve("1234567");
            requestMessage.setMsgFmt(SmsDcs.getGeneralDataCodingDcs(SmsAlphabet.RESERVED));
            requestMessage.setMessageResponseHandler(new IMessageResponseHandler() {

                @Override
                public void messageComplete(IMessage requestMsg, IMessage responseMsg) {
                    log.debug("messageComplete");
                    SmgpAtomicUtil.handlerDelay(System.currentTimeMillis() - requestMsg.getSendTimeStamp());
                }

                @Override
                public void messageExpired(String s, IMessage iMessage) {
                    log.error("messageExpired");
                    SmgpAtomicUtil.handlerDelay(System.currentTimeMillis() - iMessage.getSendTimeStamp());
                }

                @Override
                public void sendMessageFailed(IMessage iMessage) {
                    SmgpAtomicUtil.handlerDelay(System.currentTimeMillis() - iMessage.getSendTimeStamp());
                    log.error("sendMessageFailed");
                }
            });
            log.debug("SmgpSubmitRequestMessage = {}", requestMessage);
            sendMessage(requestMessage);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    private void sendMessage(SmgpSubmitRequestMessage requestMessage) {
        List<ChannelSession> sessions = PANEL.sessionManager.getSessions();
        if (sessions == null || sessions.size() == 0) {
            log.error("ChannelSession为空，无法发送短信");
            return;
        }
        SmgpClientMessageProvider messageProvider = (SmgpClientMessageProvider) PANEL.sessionManager.getMessageProvider();
        boolean hasSend = messageProvider.addMessage(requestMessage);
        if (!hasSend) {
            resendMessageDelay(requestMessage, 1000);
        }
    }

    public void resendMessageDelay(SmgpSubmitRequestMessage requestMessage, int delay) {
        CommonThreadPoolFactory.getInstance().getScheduleExecutor().schedule(() -> {
            sendMessage(requestMessage);
        }, delay, TimeUnit.MILLISECONDS);
    }

}
